package com.demo.web;

import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.Ads;
import com.demo.service.IAdsService;
import com.demo.vo.AdsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/ads")
public class AdsController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAdsService service;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Integer> create(Ads arg, HttpSession session) {

        int service_res = service.insertSelective(arg);

        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Ads> get(Ads arg, HttpSession session) {
        Ads service_res = service.selectByPrimaryKey(arg.getId());
        BaseResult<Ads> res = new BaseResult<Ads>(true, service_res);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Ads arg) {
        BaseResult<Object> res = null;
        int service_res = service.deleteByPrimaryKey(arg.getId());
        res = new BaseResult(true, service_res);
        return res;
    }
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Object> update(Ads arg) {
        BaseResult<Object> res = null;
        int service_res = service.updateByPrimaryKeySelective(arg);
        res = new BaseResult(true, service_res);
        return res;
    }


    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(AdsVo arg) {
        List<Ads> list = service.queryByPage(arg);
        int total = service.queryCount(arg);
        ListResult res = new ListResult(true, list);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        res.setPage(page);
        return res;
    }
}
